


import { sys } from "cc";
import { NetworkMgr } from "./weNetworkMgr";
import tyqSdkConfig from "./tyq-sdk-config";

//事件打点管理
export default class TyqEventMgr {
    public static tyqEventMgr: TyqEventMgr = null;
    private item_openId = "tyq_item_openid";
    private item_watermark = "tyq_item_watermark_";
    public static getInstance(): TyqEventMgr {
        if (this.tyqEventMgr == null) {
            this.tyqEventMgr = new this();
        }
        return this.tyqEventMgr;
    }
    public static get ins() {
        return this.getInstance();
    }
    //注册事件
    public onRegister(id: string) {
        console.log("event:注册");

    }
    //登陆事件
    public onLogin(id: string) {
        console.log("event:登录");

    }
    //当用户同意用户协议事件
    public onAgreeUseAgree() {
        console.log("event:同意用户协议");




    }
    /**
     * 收集广告视频行为(已弃用)
     * @param status 广告状态。1 拉取成功（视频请求pv） 2 广告展示(视频曝光pv) 3 视频完整播放 4 视频中途关掉（这个要上传play_time）
     */
    //当广告加载成功
    public onAdLoad() {
    }
    //当广告显示成功
    public onAdShow() {
    }
    //当用户完整看完广告
    public onAdSuccess() {
    }
    //当用户取消观看广告
    public onAdCancel() {
    }

    //发送普通打点事件
    public sendEvent(eventType: string, eventName?: string) {
        let name = eventName ? eventType + "-" + eventName : eventType;
        this.sendEventPoint(name);
    }
    //发送引导事件
    public sendGuidStep(step) {
        this.sendEventPoint("引导-" + step);
    }
    /**
     * 获取时间戳
     * @param second 传true 会返回精确到秒的时间戳 ，默认返回精确到毫秒的时间戳
     * @returns 
     */
    private getTimeStamp(isSecond = false) {
        let time = Date.now();
        if (isSecond) {
            time = Math.floor(time / 1000);
        }
        return time;
    }
    /**随机字符串*/
    private randomStr(strLen: number = 32) {
        let len = strLen;
        let $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
        let maxPos = $chars.length;
        let pwd = '';
        for (let idx = 0; idx < len; idx++) {
            pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
        }
        return pwd;
    }

    private randOpenid() {
        var s = [], hexDigits = "0123456789abcdef";
        for (var i = 0; i < 40; i++) {
            s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
        }
        s[14] = "4";
        s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
        s[8] = s[13] = s[18] = s[23] = s[28] = "-";
        return s.join("");
    }
    /**openID初始化 */
    public initOpenId(): string {
        let isNew = false;
        let openId = sys.localStorage.getItem(this.item_openId);
        if (!openId) {
            isNew = true;
            openId = this.randOpenid();
            sys.localStorage.setItem(this.item_openId, openId);
            this.onRegister(openId);
        }
        console.log("openid=", openId);

        return openId;
    }
    /**
     * 收集用户点击事件
     * @param event 时间名称
     */
    public collectClickEvent(event: string) {
        if (!this.initOpenId()) {
            console.error("没有openid");
            return;
        }
        var time = this.getTimeStamp(true);
        let info = {
            appid: tyqSdkConfig.appId,
            nonce_str: this.randomStr(),
            time_stamp: time,
            openid: this.initOpenId(),
            event_id: event,
            watermark: (sys.localStorage.getItem(this.item_watermark) || ""),
        }
        NetworkMgr.xhrPost("api/v1/collect_click_event", info);
    }
    private sendEventPoint(eventName) {
        this.collectClickEvent(eventName);
        console.log("%cevent:" + eventName, "color:#FF0000");


    }
    //开始游戏
    public onStartGame(level: number) {

        console.log("event:gamestart:", level);
    }
    //结束广告
    public onEndGame(isWin) {

        console.log("event:gameEnd:", isWin);
    }
}